﻿using System;
using System.Collections;
using System.Configuration;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;

using FilmAlert.Common;
using FilmAlert.BusinessLayer;

namespace FilmAlert.FilmTitleTranslatorWeb
{
    public partial class FilmFinder : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.Page.Form.DefaultButton = this.filmSearchButton.UniqueID;

            if (Page.IsPostBack)
            {
                this.filmSearchTbx.Attributes.Remove("OnClick");
            }
            else
            {
                this.filmSearchTbx.Attributes.Add("OnClick", "javascript:this.value='';");

                //this.FilmSearchTbx.Attributes.Add("onkeydown", "if ((event.which && event.which == 13) ||"
                //+ "(event.keyCode && event.keyCode == 13))"
                //+ "{theForm." + this.FilmSearchBtn.UniqueID + ".click();return false;} "
                //+ "else return true;");
                //    this.FilmSearchTbx.Attributes.Add("onkeydown", "SimulateOnClick(theForm." + this.FilmSearchBtn.UniqueID + ")");
            }
        }

        protected void filmSearchButton_Click(object sender, EventArgs e)
        {
            SearchAndDisplayResults();
        }

        private void SearchAndDisplayResults()
        {
            int totalResults = 0;

            Films films = new Search().SearchFilm(this.filmSearchTbx.Text);


            //Display the Popular Titles
            TreeNode treeNode;
            if (films.PopularTitles != null && films.PopularTitles.Count() > 0)
            {
                totalResults = films.PopularTitles.Count();

                treeNode = new TreeNode("Popular Titles: " + films.PopularTitles.Count().ToString());
                foreach (Film film in films.PopularTitles)
                {
                    treeNode.ChildNodes.Add(new TreeNode()
                    {
                        Text = film.Title + " (" + film.Year.ToString() + ")",
                        Value = film.ImdbId.ToString()
                    });
                }
                this.resultsTreeView.Nodes.Add(treeNode);
            }


            //Display the Exact matches
            if (films.ExactMatches != null && films.ExactMatches.Count() > 0)
            {
                totalResults += films.ExactMatches.Count();

                treeNode = new TreeNode("Exact matches: " + films.ExactMatches.Count().ToString());
                foreach (Film film in films.ExactMatches)
                {
                    treeNode.ChildNodes.Add(new TreeNode()
                    {
                        Text = film.Title + " (" + film.Year.ToString() + ")",
                        Value = film.ImdbId.ToString()
                    });
                }
                this.resultsTreeView.Nodes.Add(treeNode);
            }

            //Display the Approx matches
            if (films.ApproxTitles != null && films.ApproxTitles.Count() > 0)
            {
                totalResults += films.ApproxTitles.Count();

                treeNode = new TreeNode("Approx. matches: " + films.ApproxTitles.Count().ToString());
                foreach (Film film in films.ApproxTitles)
                {
                    treeNode.ChildNodes.Add(new TreeNode()
                    {
                        Text = film.Title + " (" + film.Year.ToString() + ")",
                        Value = film.ImdbId.ToString()
                    });
                }
                this.resultsTreeView.Nodes.Add(treeNode);
            }


            //Display the Translation (Akas) matches
            if (films.Akas.Count() > 0)
            {
                totalResults += films.Akas.Count();

                treeNode = new TreeNode("Translations: " + films.Akas.Count().ToString());
                foreach (Aka film in films.Akas)
                {
                    treeNode.ChildNodes.Add(new TreeNode()
                    {
                        Text = film.Title,
                        Value = film.ImdbId.ToString()
                    });
                }
                this.resultsTreeView.Nodes.Add(treeNode);
            }

            //Display no found message depending on totalResults
            this.noResultsLabel.Visible = (totalResults == 0);

            //Expand the first node and select the first result.
            if (resultsTreeView.Nodes.Count > 0)
            {
                this.resultsTreeView.Visible = true;
                this.resultsTreeView.Nodes[0].Expand();
          //      resultsTreeView.Nodes[0].ChildNodes[0].Select();
            }
        }

        protected void resultsTreeView_SelectedNodeChanged(object sender, EventArgs e)
        {
            TreeNode selectedTreeNode = (sender as TreeView).SelectedNode;
            if (selectedTreeNode.Parent == null)
            {
                if ((bool)selectedTreeNode.ChildNodes[0].Expanded)
                    selectedTreeNode.Collapse();
                else
                    selectedTreeNode.Expand();
            }
            else
                DisplayDetail(Convert.ToInt32(selectedTreeNode.Value));

        }

        private void DisplayDetail(Int32 imdbId)
        {
            this.resultPanel.Visible = true;

            Film film = new FilmDetails().GetFilmDetails(imdbId);

            this.filmTitleTextLabel.Text = film.Title;
            if (film.Year > 0)
                this.filmTitleTextLabel.Text += " ("+film.Year.ToString()+")";

            if (film.TvFilm)
                this.filmTitleTextLabel.Text += " (Tv film)";

            if (film.Directors!=null && film.Directors.Count > 0)
            {
                this.filmDirectorTextLabel.Text = " - by ";
                foreach (Director director in film.Directors)
                {
                    this.filmDirectorTextLabel.Text += director.Name +",";
                }
                this.filmDirectorTextLabel.Text = this.filmDirectorTextLabel.Text.Substring(0, this.filmDirectorTextLabel.Text.Length - 1);
            }

            this.actorsGridView.DataSource = film.Actors;

            this.filmAkasGrid.DataSource = new Translations().GetTitleTranslations(imdbId);
            
            //Bind for everything!
            this.DataBind();
        }
    }
}
